package com.situ.Servlet.ServletContest;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.jar.Attributes.Name;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.RespectBinding;

import com.situ.student.entity.Student;

public class ServletContest_2 extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html; charset=utf8");
		ServletContext servletContext = getServletContext();
		
		/**
		 * 取出1中的人，与学生列表
		 */
		String name = (String) servletContext.getAttribute("name");//取出contest1中的马柳
		List<Student> list = (List<Student>) servletContext.getAttribute("list");
		
		/**
		 * 访问页面人数+1，借此了解ServleContext域
		 * 
		 * InitSrevlet全局初始化后，不用if了
		 */
		Integer count = (Integer) servletContext.getAttribute("count");
//		if (count == null) {
//			count = 0;
//		}else {
			count = count + 1;
//		}
		servletContext.setAttribute("count", count);
		
		//打印
		PrintWriter writer = resp.getWriter();
		writer.println("访客" + name + ": "  + count);
		for (Student student : list) { //展示学生列表
			writer.println(student);
		}
		writer.close();
	}
}
